package com.gitlab.bootren.data.core.query;

import com.gitlab.bootren.data.core.query.restriction.RestrictionType;

import java.util.HashMap;
import java.util.Map;

/**
 * 键值对类型条件
 *
 * @author tengda
 *
 *
 */
class KvCondition extends AbstractCondition {

    protected String key;

    protected Object value;

    protected RestrictionType restrictionType = RestrictionType.EQ;

    private String paramName = null;

    /**
     * 构建一个键值对条件
     *
     * @param key 字段名
     * @param value 字段值
     */
    public KvCondition(String key, Object value) {
        this.key = key;
        this.value = value;
        this.paramName = key.replaceAll("\\.", "_");

    }

    /**
     * 构建一个键值对条件
     *
     * @param key 字段名
     * @param value 字段值
     * @param restrictionType 约束类型
     */
    public KvCondition(String key, Object value, RestrictionType restrictionType) {

        this(key, value);
        this.restrictionType = restrictionType;

    }

    @Override
    public String ql() {
        String ql = this.restrictionType.toQl(key, value, paramName);
        return this.replaceAlias(ql, "");
    }

    @Override
    public String ql(String aliasName) {
        String ql = this.restrictionType.toQl(key, value, paramName);
        return this.replaceAlias(ql, aliasName);
    }

    @Override
    public Map<String, Object> getParams() {
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put(this.paramName, value);
        return param;
    }

    @Override
    public ICondition setParams(Map<String, Object> params) {
        return this;
    }

    @Override
    public ICondition setParam(String paramName, Object object) {
        return this;
    }

    public String getParamName() {
        return paramName;
    }

    public KvCondition setParamName(String paramName) {
        this.paramName = paramName;
        return this;
    }

    public KvCondition copy() {
        return new KvCondition(this.key, this.value, this.restrictionType);
    }

}
